package com.csh.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.csh.subject.common.enums.IsDeletedFlagEnum;
import com.csh.subject.domain.convert.SubjectCategoryConvert;
import com.csh.subject.domain.entity.SubjectCategoryBo;
import com.csh.subject.domain.service.SubjectCategoryDomainService;
import com.csh.subject.infra.basic.entity.SubjectCategory;
import com.csh.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {
    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public void add(SubjectCategoryBo subjectCategoryBo) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.add.Bo:{}", JSON.toJSONString(subjectCategoryBo));
        }
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE
                .convertBoToCategory(subjectCategoryBo);
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        subjectCategoryService.insert(subjectCategory);
    }


    @Override
    public List<SubjectCategoryBo> queryCategory(SubjectCategoryBo subjectCategoryBo) {
        SubjectCategory subjectCategory = SubjectCategoryConvert
                .INSTANCE.convertBoToCategory(subjectCategoryBo);
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
        List<SubjectCategoryBo> boList = SubjectCategoryConvert.INSTANCE
                .convertCategoryToBo(subjectCategoryList);
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.queryPrimaryCategory.boList:{}",
                    JSON.toJSONString(boList));
        }
        boList.stream().map(
                bo -> {
                    Integer subjectCount = subjectCategoryService.querySubjectCount(bo.getId());
                    bo.setCount(subjectCount);
                    return bo;
                }
        ).collect(Collectors.toList());
        return boList;
    }


    @Override
    public Boolean update(SubjectCategoryBo subjectCategoryBo) {
        SubjectCategory subjectCategory = SubjectCategoryConvert
                .INSTANCE.convertBoToCategory(subjectCategoryBo);
        int updateCount = subjectCategoryService.update(subjectCategory);
        return updateCount > 0;
    }

    @Override
    public Boolean delete(SubjectCategoryBo subjectCategoryBo) {
        SubjectCategory subjectCategory = SubjectCategoryConvert
                .INSTANCE.convertBoToCategory(subjectCategoryBo);
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int updateCount = subjectCategoryService.update(subjectCategory);
        return updateCount > 0;
    }
}
